<?php
/**
 * Miscellaneous Theme Functions
 */

/**
 * Register widgetized areas, including two sidebars and six widget-ready columns in the footer.
 *
 */
function webtreats_widgets_init() {
	register_sidebar( array(
		'name' =>  'Primary Widget Area',
		'description' => 'The primary widget area',
		'before_widget' => '<div id="%1$s" class="widget %2$s">',
		'after_widget' => '</div>',
		'before_title' => '<h3 class="widgettitle">',
		'after_title' => '</h3>',
	) );

	register_sidebar( array(
		'name' => 'Homepage Widget Area',
		'description' => 'The homepage widget area',
		'before_widget' => '<div id="%1$s" class="widget %2$s">',
		'after_widget' => '</div>',
		'before_title' => '<h3 class="widgettitle">',
		'after_title' => '</h3>',
	) );

	register_sidebar( array(
		'name' => 'First Footer Widget Area',
		'description' => 'The first footer widget area',
		'before_widget' => '<div id="%1$s" class="widget %2$s">',
		'after_widget' => '</div>',
		'before_title' => '<h6 class="footer_widgettitle">',
		'after_title' => '</h6>',
	) );

	register_sidebar( array(
		'name' => 'Second Footer Widget Area',
		'description' => 'The second footer widget area',
		'before_widget' => '<div id="%1$s" class="widget %2$s">',
		'after_widget' => '</div>',
		'before_title' => '<h6 class="footer_widgettitle">',
		'after_title' => '</h6>',
	) );

	register_sidebar( array(
		'name' => 'Third Footer Widget Area',
		'description' => 'The third footer widget area',
		'before_widget' => '<div id="%1$s" class="widget %2$s">',
		'after_widget' => '</div>',
		'before_title' => '<h6 class="footer_widgettitle">',
		'after_title' => '</h6>',
	) );

	register_sidebar( array(
		'name' => 'Fourth Footer Widget Area',
		'description' => 'The fourth footer widget area',
		'before_widget' => '<div id="%1$s" class="widget %2$s">',
		'after_widget' => '</div>',
		'before_title' => '<h6 class="footer_widgettitle">',
		'after_title' => '</h6>',
	) );
	
	register_sidebar( array(
		'name' => 'Fifth Footer Widget Area',
		'description' => 'The fifth footer widget area',
		'before_widget' => '<div id="%1$s" class="widget %2$s">',
		'after_widget' => '</div>',
		'before_title' => '<h6 class="footer_widgettitle">',
		'after_title' => '</h6>',
	) );
	
	register_sidebar( array(
		'name' => 'Sixth Footer Widget Area',
		'description' => 'The sixth footer widget area',
		'before_widget' => '<div id="%1$s" class="widget %2$s">',
		'after_widget' => '</div>',
		'before_title' => '<h6 class="footer_widgettitle">',
		'after_title' => '</h6>',
	) );
}
/** Register sidebars by running webtreats_widgets_init() on the widgets_init hook. */
add_action( 'widgets_init', 'webtreats_widgets_init' );


function new_more_link( $more_link, $more_link_text ) {
	
	$more_link = '[raw]' .$more_link. '[/raw]';
	
	return str_replace('more-link', 'fancy_link read_more_link', $more_link );
}
add_filter('the_content_more_link', 'new_more_link', 10, 2);


function new_excerpt_more($excerpt) {
	return str_replace('[...]', '...', $excerpt);
}
add_filter('wp_trim_excerpt', 'new_excerpt_more');


function new_comment_author_link($return) {
	return str_replace($return, "<span></span>$return", $return);
}
add_filter('get_comment_author_link', 'new_comment_author_link');


function webtreats_excerpt($length, $ellipsis) {
	$text = get_the_content();
	$text = preg_replace('`\[(.*)]*\]`','',$text);
	$text = strip_tags($text);
	$text = substr($text, 0, $length);
	$text = substr($text, 0, strripos($text, " "));
	$text = $text.$ellipsis;
	return $text;
}


function webtreats_truncate($string, $limit, $break=".", $pad="...") {
	if(strlen($string) <= $limit) return $string;
	
	 if(false !== ($breakpoint = strpos($string, $break, $limit))) {
		if($breakpoint < strlen($string) - 1) {
			$string = substr($string, 0, $breakpoint) . $pad;
		}
	  }
	return $string; 
}


function webtreats_image_resize($height,$width,$img_url) {
	global $blog_id;

	$image['url'] = $img_url;
	$image_path = explode($_SERVER['SERVER_NAME'], $image['url']);
	$image_path = $_SERVER['DOCUMENT_ROOT'] . $image_path[1];
	$image_info = @getimagesize($image_path);

	// If we cannot get the image locally, try for an external URL
	if (!$image_info)
		$image_info = @getimagesize($image['url']);

	$image['width'] = $image_info[0];
	$image['height'] = $image_info[1];
	if($img_url != "" && ($image['width'] != $width || $image['height'] != $height || !isset($image['width']))){
		
		//If WP MU
		if ( (defined('WP_ALLOW_MULTISITE')) && (WP_ALLOW_MULTISITE == true) ) {
			$the_image_src = $img_url;
			if (isset($blog_id) && $blog_id > 0) {
				$image_parts = explode('/files/', $the_image_src);
				if (isset($image_parts[1])) {
					$the_image_src = '/blogs.dir/' . $blog_id . '/files/' . $image_parts[1];
					$the_imasdfage_src = $image_parts[0];
					$mu_image_path = $the_imasdfage_src. '/wp-content' .$the_image_src;
				}else{
					$mu_image_path = $image_parts[0];
				}
			}
		
			$img_url = WEBTREATS_SCRIPTS_FOLDER."/thumb.php?src=$mu_image_path&amp;w=$width&amp;h=$height&amp;zc=1&amp;q=100";
		}else{
			$img_url = WEBTREATS_SCRIPTS_FOLDER."/thumb.php?src=$img_url&amp;w=$width&amp;h=$height&amp;zc=1&amp;q=100";	
		}
	}
	
	return $img_url;
}



function webtreats_image_format($img_url, $class, $alt, $title) {

	$image['url'] = $img_url;
	$image_path = explode($_SERVER['SERVER_NAME'], $image['url']);
	$image_path = $_SERVER['DOCUMENT_ROOT'] . $image_path[1];
	$image_info = @getimagesize($image_path);

	// If we cannot get the image locally, try for an external URL
	if (!$image_info)
		$image_info = @getimagesize($image['url']);

	$image['width'] = $image_info[0];
	$image['height'] = $image_info[1];
	
	return '<img class="' .$class. ' framed" height="' .$image['height']. '" width="' .$image['width']. '" alt="' .$alt. '" title="' .$title. '" src="' . $img_url . '" />';
}


function webtreats_register_menus() {
	global $themename;
	
	//If WP 3.0 or > include supprot for wp_nav_menu()
	if(webtreats_check_wp_version()){
		register_nav_menus(
			array(
				'primary-menu' => __( $themename. ' Menu' ),
			)
		);
	}
	
}
add_action( 'init', 'webtreats_register_menus' );


function webtreats_menu() {
	global $show_hide_pg;
	
	//If WP 3.0 or > include supprot for wp_nav_menu()
	if(webtreats_check_wp_version()){
		
		if ( has_nav_menu( 'primary-menu' ) ) {
			wp_nav_menu( array( 'menu' => 'primary-menu', 'container_id' => 'main_navigation', 'container_class' =>  'jqueryslidemenu unitPng', 'menu_class' => '', 'fallback_cb' => '' ) );
			return;
		}
	}
	
	//Exclude a parent and all of that parent's child Pages
	if($show_hide_pg) {
		$parent_pages_to_exclude = explode(",", $show_hide_pg);
		foreach($parent_pages_to_exclude as $parent_page_to_exclude) {
			if ($page_exclusions) { $page_exclusions .= ',' . $parent_page_to_exclude;
			}else{
			$page_exclusions = $parent_page_to_exclude; }
			$descendants = get_pages('child_of=' . $parent_page_to_exclude);
			if($descendants){
				foreach($descendants as $descendant) {
					$page_exclusions .= ',' . $descendant->ID;
				}
			}
		}	
	}

	//If !WP 3.0 or no menu created use wp_list_pages()
	$active_class = (is_front_page()) ? 'class="current_page_item"' : '';

	$out .= '<div id="main_navigation" class="jqueryslidemenu unitPng">';
	$out .= '<ul>';
	$out .= '<li><a ' .$active_class. ' href="' .get_option('home'). '">Home</a></li>';

	$out .= wp_list_pages("sort_column=menu_order&exclude=$page_exclusions&title_li=&echo=0");
	$out .= '</ul>';
	$out .= '</div>';
	
	echo $out;
		
}


function webtreats_social_header($variation_imgs) {
	global $header_contact;
	
	require(WEBTREATS_INCLUDES . "/var.php");
	
	if( (!$twitter_sociable_disable) || (!$facebook_sociable_disable) || (!$email_sociable_disable) || (!$rss_sociable_disable) ){
	
		$style = ($header_contact) ? '' : ' style="top:35px;"';
	
		$out .= '<div id="social_header"' .$style. '><div class="social_header_background">';
	
		if(!$twitter_sociable_disable){
			$twitter_sociable = (!$twitter_sociable) ? 'http://twitter.com' : 'http://twitter.com/'.$twitter_sociable;
			$out .= '<div class="fade_hover"><a class="target_blank" href="' .$twitter_sociable. '"><img alt="" class="unitPng" src="' .$variation_imgs. '/header_sociables_twitter.png" height="22px" width="24px" /></a></div>';
		}
	
		if(!$facebook_sociable_disable){
			$facebook_sociable = (!$facebook_sociable) ? 'http://www.facebook.com' : 'http://www.facebook.com/'.$facebook_sociable;
			$out .= '<div class="fade_hover"><a class="target_blank" href="' .$facebook_sociable. '"><img alt="" class="unitPng" src="' .$variation_imgs. '/header_sociables_facebook.png" height="22px" width="24px" /></a></div>';
		}
		
		if(!$linkedin_sociable_disable){
			$linkedin_sociable = (!$linkedin_sociable) ? 'http://www.linkedin.com' : $linkedin_sociable;
			$out .= '<div class="fade_hover"><a class="target_blank" href="' .$linkedin_sociable. '"><img alt="" class="unitPng" src="' .$variation_imgs. '/header_sociables_linkedin.png" height="22px" width="24px" /></a></div>';
		}
		
		if(!$myspace_sociable_disable){
			$myspace_sociable = (!$myspace_sociable) ? 'http://www.myspace.com' : 'http://www.myspace.com/'.$myspace_sociable;
			$out .= '<div class="fade_hover"><a class="target_blank" href="' .$myspace_sociable. '"><img alt="" class="unitPng" src="' .$variation_imgs. '/header_sociables_myspace.png" height="22px" width="24px" /></a></div>';
		}
		
		if(!$delicious_sociable_disable){
			$delicious_sociable = (!$delicious_sociable) ? 'http://delicious.com' : 'http://delicious.com/'.$delicious_sociable;
			$out .= '<div class="fade_hover"><a class="target_blank" href="' .$delicious_sociable. '"><img alt="" class="unitPng" src="' .$variation_imgs. '/header_sociables_delicious.png" height="22px" width="24px" /></a></div>';
		}
	
		if(!$email_sociable_disable){
			$email_sociable = (!$email_sociable) ? get_option('admin_email') : $email_sociable;
			$out .= '<div class="fade_hover"><a href="#" rel="' .webtreats_nospam($email_sociable). '" class="email"><img alt="" class="unitPng" src="' .$variation_imgs. '/header_sociables_email.png" height="22" width="24" /></a></div>';
		}
	
		if(!$rss_sociable_disable){
			$rss_sociable = (!$rss_sociable) ? get_bloginfo('rss2_url') : $rss_sociable;
			$out .= '<div class="fade_hover"><a href=" ' .$rss_sociable. '"><img alt="" class="unitPng" src="' .$variation_imgs. '/header_sociables_rss.png" height="22px" width="24px" /></a></div>';
		}
		
		if($custom_1_img_sociable){
			$out .= '<div class="fade_hover"><a class="target_blank" href="' .$custom_1_sociable. '"><img alt="" class="unitPng" src="' .$custom_1_img_sociable. '" height="22px" width="24px" /></a></div>';
		}
		
		if($custom_2_img_sociable){
			$out .= '<div class="fade_hover"><a class="target_blank" href="' .$custom_2_sociable. '"><img alt="" class="unitPng" src="' .$custom_2_img_sociable. '" height="22px" width="24px" /></a></div>';
		}
	
		$out .= '</div></div>';
	
		echo $out;
	}
}


function webtreats_title($post_id) {
	global $blog_page;
	
	if(is_single()){
		
		$portfolio_full = get_post_meta($post_id, 'portfolio_full_img',true);
		$portfolio_title_disable = get_post_meta($post_id, 'portfolio_title_disable',true);
		$blog_title_disable = get_post_meta($blog_page, 'disable_title',true);
		
		if(!$portfolio_full && !$blog_title_disable){
			$pg_title = get_the_title($blog_page);
			
			return '<h1>' .$pg_title. '</h1>';
		}
		
		if($portfolio_full && !$portfolio_title_disable){
			$category = get_the_category($post_id); 
			$pg_title = $category[0]->cat_name;
			
			return '<h1>' .$pg_title. '</h1>';
		}
		
		return;
	}
	
	
	if(is_page()){
		$disable_title = get_post_meta($post_id, 'disable_title',true);
		
		if(!$disable_title){
			$pg_title = get_the_title($post_id);
			return '<h1>' .$pg_title. '</h1>';
		}
		
		return;
	}
	
}


/**
 * webtreats_teaser_text() - outputs HTML for teaser text
 * 
 * @param int $post_id Post ID
 */
function webtreats_teaser_text($post_id) {
	global $header_teaser, $teaser_custom, $blog_page, $twitter_id;
	
	//get_post_meta teaser text settings
	$teaser_text = (get_post_meta($post_id, 'teaser_text', true)) ? get_post_meta($post_id, 'teaser_text', true) : 'default';
	$teaser_text_custom = get_post_meta($post_id, 'teaser_text_custom', true);
	
	//teaser text disabled
	if ( !is_search() ) {
		if( $teaser_text == 'default' && $header_teaser == 'disable' || $teaser_text == 'disable' ){
			return;
		}
	}
	
	//if page is 404
	if ( is_404() ) {
	    echo '<div id="intro_blurb">';
		echo '<div class="inner">';
		echo '<h1>404 - Not Found</h1>';
		echo '<div id="blurb">';
		echo "Looks like the page you're looking for isn't here anymore. Try using the search box or sitemap below.";
		echo '</div>';
		echo '</div>';
		echo '</div><!-- intro_blurb -->';
		echo '<div class="clearboth"></div>';
		
		return;
	}
	
	//if page is search
	if ( is_search() ) {
		
		if($header_teaser == 'disable'){ return; }
		
	    echo '<div id="intro_blurb">';
		echo '<div class="inner">';
		echo '<h1>Search</h1>';
		echo '<div id="blurb">';
		echo 'Search Results for: &lsquo;'. get_search_query() .'&rsquo;';
		echo '</div>';
		echo '</div>';
		echo '</div><!-- intro_blurb -->';
		echo '<div class="clearboth"></div>';
		
		return;
	}
	
	//teaser text meta override
	if( $teaser_text != 'default' && $teaser_text != 'disable'){
		
		echo '<div id="intro_blurb">';
		echo '<div class="inner">';
		echo webtreats_title($post_id);
		
		if ($teaser_text == 'custom' && $teaser_text_custom) {
			echo '<div id="blurb">';
			$teaser_text_custom = stripslashes($teaser_text_custom); echo do_shortcode($teaser_text_custom);
			echo '</div>';
		}
		if ($teaser_text == 'twitter' && !$twitter_id) {
			echo '<div id="blurb">';
			echo 'You must have your Twitter user name entered in the "General Settings" tab of your themes options for this to function properly.';
			echo '</div>';
		}
		if ($teaser_text == 'twitter' && $twitter_id) {
			echo '<div id="blurb">';
			$usernames = $twitter_id; $limit = '1'; $type = 'teaser';
			echo parse_cache_twitter_feed($usernames, $limit, $type);
			echo '</div>';
		}
		
		echo '</div>';
		echo '</div><!-- intro_blurb -->';
		echo '<div class="clearboth"></div>';
	}
	
	
	//teaser text default
	if( $header_teaser != 'disable' && $teaser_text == 'default' ){
		
		echo '<div id="intro_blurb">';
		echo '<div class="inner">';
		echo webtreats_title($post_id);
		
		if ($header_teaser == 'customtext' && $teaser_custom) {
			echo '<div id="blurb">';
			$teaser_custom = stripslashes($teaser_custom); echo do_shortcode($teaser_custom);
			echo '</div>';
		}
		if ($header_teaser == 'twitter' && !$twitter_id) {
			echo '<div id="blurb">';
			echo 'You must have your Twitter user name entered in the "General Settings" tab of your themes options for this to function properly.';
			echo '</div>';
		}
		if ($header_teaser == 'twitter' && $twitter_id) {
			echo '<div id="blurb">';
			$usernames = $twitter_id; $limit = '1'; $type = 'teaser';
			echo parse_cache_twitter_feed($usernames, $limit, $type);
			echo '</div>';
		}
		
		echo '</div>';
		echo '</div><!-- intro_blurb -->';
		echo '<div class="clearboth"></div>';
	}
	
}


function webtreats_post_image(){
	global $post;
	
	$post_image = get_post_meta($post->ID, 'post_image', true);
	
	if($post_image){
		
		if(preg_match_all('!.+\.(?:jpe?g|png|gif)!Ui',$post_image,$matches)){ ?>

		<?php 
		$post_image_resized = webtreats_image_resize($img_height=240, $img_width=568, $post_image); ?>

		<?php 
		$bg_hover = (is_single()) ? ' bg_hover' : '';
		$post_link = (is_single()) ? $post_image : get_permalink( $post->ID ); ?>
			
			<div class="img_loader blog_post_image<?php echo $bg_hover; ?>">
				<a class="load_img"<?php if(is_single()){echo ' rel="lightbox"';} ?> title="<?php the_title(); ?>" href="<?php echo $post_link; ?>">
					<span class="rm_img noscript"><img class="framed fade_hover" src="<?php echo $post_image_resized; ?>" height="<?php echo $img_height; ?>" width="<?php echo $img_width; ?>"  alt="" /></span>
				</a>
			</div>
			
		<?php }

		if(preg_match_all('#http://(www.vimeo|vimeo)\.com(/|/clip:)(\d+)(.*?)#i',$post_image,$matches)){
			$post_image = '[vimeo height="240" width="568"]' .$post_image. '[/vimeo]';
			
			echo '<div class="blog_post_video">' .do_shortcode($post_image). '</div>';
		}

		if(preg_match( '#http://(www.youtube|youtube|[A-Za-z]{2}.youtube)\.com/(.*?)#i', $post_image, $matches )){
			$post_image = '[youtube height="240" width="568"]' .$post_image. '[/youtube]';
			
			echo '<div class="blog_post_video">' .do_shortcode($post_image). '</div>';
		}
		
	}
	
}


function webtreats_contact_form($email) {

	$email_adress_reciever = $email != "" ? $email : get_option('admin_email');
	
	//If the form is submitted
	if(isset($_POST['submittedContact'])) {
		require(WEBTREATS_INCLUDES . "/submit.php");
	}
	
	if(isset($emailSent) && $emailSent == true) {
		
		$out .= '<a name="contact_"></a>';
		$out .= '<p class="thanks"><strong>Thanks!</strong> Your email was successfully sent.</p>';
		
	} else {
		
		if(isset($captchaError)) {
			$out .= '<a name="contact_"></a>';
			$out .= '<p class="error">There was an error submitting the form.<p>';
		}
		
		$out .= '<a name="contact_"></a>';
		$out .= '<form action="' .get_permalink(). '#contact_" id="contact_form" method="post">';
		$out .= '[raw]<p><input type="text" name="contactName" id="contactName" value="';
		
		if(isset($_POST['contactName'])) {
			$out .= $_POST['contactName'];
		}
		$out .= '"';
		$out .= ' class="requiredFieldContact textfield';
		
		if($emailError != '') {
			$out .= ' inputError';
		}
		$out .= '"';
		$out .= ' size="22" tabindex="1" /><label class="textfield_label" for="contactName">Name *</label></p>[/raw]';
		
		$out .= '<p><input type="text" name="email" id="email" value="';
		
		if(isset($_POST['email'])) {
			$out .= $_POST['email'];
		}
		$out .= '"';
		$out .= ' class="requiredFieldContact email textfield';
		
		if($emailError != '') {
			$out .= ' inputError';
		}
		$out .= '"';
		$out .= ' size="22" tabindex="2" /><label class="textfield_label" for="email">Email *</label></p>';
		
		$out .= '<p><textarea name="comments" id="commentsText" rows="20" cols="30" tabindex="3" class="requiredFieldContact textarea';
		
		if($commentError != '') {
			$out .= ' inputError';
		}
		$out .= '">';
		
		if(isset($_POST['comments'])) { 
			if(function_exists('stripslashes')) { 
				$out .= stripslashes($_POST['comments']); 
				} else { 
					$out .= $_POST['comments']; 
				} 
			}
		$out .= '</textarea></p>';
		
		$out .= '<p class="screenReader"><label for="checking" class="screenReader">If you want to submit this form, do not enter anything in this field</label><input type="text" name="checking" id="checking" class="screenReader" value="';
		
		 if(isset($_POST['checking'])) {
			echo $_POST['checking'];
		}
		$out .= '" /></p>';
		
		$out .= '<p class="loadingImg" style="display:none;"><img src="' .WEBTREATS_IMAGES. '/classic-loader.gif" alt="" /></p>';
		$out .= '<p><button name="submittedContact" id="submittedContact" type="submit" class="fancy_button" tabindex="4" value="Submit" ><span>Submit</span></button></p>';
		
		$out .= '<p class="screenReader"><input id="submitUrl" type="hidden" name="submitUrl" value="' .get_template_directory_uri(). '/lib/includes/submit.php" /></p>';
		$out .= '<p class="screenReader"><input id="emailAddress" type="hidden" name="emailAddress" value="' .webtreats_nospam($email_adress_reciever). '" /></p>';
	
		$out .= '</form>';

	}
	return $out;
}


function webtreats_portfolio_pagenavi($before = '', $after = '', $portfolio_query, $paged) {
	global $wpdb, $wp_query;
	if (!is_single()) {
		$request = $wp_query->request;
		$posts_per_page = 2;
		$paged = intval(get_query_var('paged'));
		$pagenavi_options = get_option('pagenavi_options');
		$numposts = $portfolio_query->found_posts;
		$max_page = $portfolio_query->max_num_pages;
		if(empty($paged) || $paged == 0) {
			$paged = 1;
		}
		$pages_to_show = intval($pagenavi_options['num_pages']);
		$larger_page_to_show = intval($pagenavi_options['num_larger_page_numbers']);
		$larger_page_multiple = intval($pagenavi_options['larger_page_numbers_multiple']);
		$pages_to_show_minus_1 = $pages_to_show - 1;
		$half_page_start = floor($pages_to_show_minus_1/2);
		$half_page_end = ceil($pages_to_show_minus_1/2);
		$start_page = $paged - $half_page_start;
		if($start_page <= 0) {
			$start_page = 1;
		}
		$end_page = $paged + $half_page_end;
		if(($end_page - $start_page) != $pages_to_show_minus_1) {
			$end_page = $start_page + $pages_to_show_minus_1;
		}
		if($end_page > $max_page) {
			$start_page = $max_page - $pages_to_show_minus_1;
			$end_page = $max_page;
		}
		if($start_page <= 0) {
			$start_page = 1;
		}
		$larger_per_page = $larger_page_to_show*$larger_page_multiple;
		$larger_start_page_start = (n_round($start_page, 10) + $larger_page_multiple) - $larger_per_page;
		$larger_start_page_end = n_round($start_page, 10) + $larger_page_multiple;
		$larger_end_page_start = n_round($end_page, 10) + $larger_page_multiple;
		$larger_end_page_end = n_round($end_page, 10) + ($larger_per_page);
		if($larger_start_page_end - $larger_page_multiple == $start_page) {
			$larger_start_page_start = $larger_start_page_start - $larger_page_multiple;
			$larger_start_page_end = $larger_start_page_end - $larger_page_multiple;
		}
		if($larger_start_page_start <= 0) {
			$larger_start_page_start = $larger_page_multiple;
		}
		if($larger_start_page_end > $max_page) {
			$larger_start_page_end = $max_page;
		}
		if($larger_end_page_end > $max_page) {
			$larger_end_page_end = $max_page;
		}
		if($max_page > 1 || intval($pagenavi_options['always_show']) == 1) {
			$pages_text = str_replace("%CURRENT_PAGE%", number_format_i18n($paged), $pagenavi_options['pages_text']);
			$pages_text = str_replace("%TOTAL_PAGES%", number_format_i18n($max_page), $pages_text);
			$out .= $before.'<div class="wp-pagenavi">'."\n";
			switch(intval($pagenavi_options['style'])) {
				case 1:
					if(!empty($pages_text)) {
						$out .= '<span class="pages">'.$pages_text.'</span>';
					}
					if ($start_page >= 2 && $pages_to_show < $max_page) {
						$first_page_text = str_replace("%TOTAL_PAGES%", number_format_i18n($max_page), $pagenavi_options['first_text']);
						$out .= '<a href="'.clean_url(get_pagenum_link()).'" class="first" title="'.$first_page_text.'">'.$first_page_text.'</a>';
						if(!empty($pagenavi_options['dotleft_text'])) {
							$out .= '<span class="extend">'.$pagenavi_options['dotleft_text'].'</span>';
						}
					}
					if($larger_page_to_show > 0 && $larger_start_page_start > 0 && $larger_start_page_end <= $max_page) {
						for($i = $larger_start_page_start; $i < $larger_start_page_end; $i+=$larger_page_multiple) {
							$page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), $pagenavi_options['page_text']);
							$out .= '<a href="'.clean_url(get_pagenum_link($i)).'" class="page" title="'.$page_text.'">'.$page_text.'</a>';
						}
					}
					$out .= get_previous_posts_link($pagenavi_options['prev_text']);
					for($i = $start_page; $i  <= $end_page; $i++) {						
						if($i == $paged) {
							$current_page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), $pagenavi_options['current_text']);
							$out .= '<span class="current">'.$current_page_text.'</span>';
						} else {
							$page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), $pagenavi_options['page_text']);
							$out .= '<a href="'.clean_url(get_pagenum_link($i)).'" class="page" title="'.$page_text.'">'.$page_text.'</a>';
						}
					}
					$out .= get_next_posts_link($pagenavi_options['next_text'], $max_page);
					if($larger_page_to_show > 0 && $larger_end_page_start < $max_page) {
						for($i = $larger_end_page_start; $i <= $larger_end_page_end; $i+=$larger_page_multiple) {
							$page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), $pagenavi_options['page_text']);
							$out .= '<a href="'.clean_url(get_pagenum_link($i)).'" class="page" title="'.$page_text.'">'.$page_text.'</a>';
						}
					}
					if ($end_page < $max_page) {
						if(!empty($pagenavi_options['dotright_text'])) {
							$out .= '<span class="extend">'.$pagenavi_options['dotright_text'].'</span>';
						}
						$last_page_text = str_replace("%TOTAL_PAGES%", number_format_i18n($max_page), $pagenavi_options['last_text']);
						$out .= '<a href="'.clean_url(get_pagenum_link($max_page)).'" class="last" title="'.$last_page_text.'">'.$last_page_text.'</a>';
					}
					break;
				case 2;
					$out .= '<form action="'.htmlspecialchars($_SERVER['PHP_SELF']).'" method="get">'."\n";
					$out .= '<select size="1" onchange="document.location.href = this.options[this.selectedIndex].value;">'."\n";
					for($i = 1; $i  <= $max_page; $i++) {
						$page_num = $i;
						if($page_num == 1) {
							$page_num = 0;
						}
						if($i == $paged) {
							$current_page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), $pagenavi_options['current_text']);
							$out .= '<option value="'.clean_url(get_pagenum_link($page_num)).'" selected="selected" class="current">'.$current_page_text."</option>\n";
						} else {
							$page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), $pagenavi_options['page_text']);
							$out .= '<option value="'.clean_url(get_pagenum_link($page_num)).'">'.$page_text."</option>\n";
						}
					}
					$out .= "</select>\n";
					$out .= "</form>\n";
					break;
			}
			$out .= '</div>'.$after."\n";
		}
	}
	return $out;
}


function webtreats_exclude_category_feed() {
	global $exclude_blog_cats;
	
	$exclude_blog_cats = preg_replace("!(\d)+!","-${0}$0", $blog_excludecats);
	if ( is_feed() ) {
		set_query_var("cat", "$exclude_blog_cats");
	}
}
add_filter('pre_get_posts', 'webtreats_exclude_category_feed');


function webtreats_body_class() {
	global $post, $homepage_slider, $home_slider_page, $blog_page, $teaser_style, $content, $color_scheme;
	
	$is_homepage_slider = ($home_slider_page) ? $homepage_slider .' ': '';
	
	$get_template = get_post_meta($post->ID, '_wp_page_template', true);
	
	$overflow = ( stristr( $post->post_content, '[tooltip' ) ) ? ' overflow_visible': '';
	
	$color_scheme = str_replace('.css','', $color_scheme);
	
	if(is_home()){
		$overflow = ( stristr( $content, '[tooltip' ) ) ? ' overflow_visible': '';
		$homepage_sidebar = (is_active_sidebar(2)) ? 'right_sidebar ' : 'full_width ';
		$body_class = 'class="' .$homepage_sidebar.$homepage_slider. ' ' .$teaser_style.$overflow. ' ' .$color_scheme. '"';
		echo $body_class;
		
		return;
	}
	
	if(is_page() && $post->ID != $blog_page){
		$sidebar = 'right_sidebar ';
		
		$is_home_slider = ($home_slider_page) ? $is_homepage_slider .' ' : '';
		
		$sidebar = ($get_template == 'template-full.php') ? 'full_width ' : $sidebar;
		$sidebar = ($get_template == 'template-left-sidebar.php') ? 'left_sidebar ' : $sidebar;
		$sidebar = (is_webtreats_portfolio_gallery($post->ID)) ? 'full_width ' : $sidebar;
				
		$body_class = 'class="' .$sidebar.$is_home_slider.$teaser_style.$overflow. ' ' .$color_scheme. '"';
		echo $body_class;
		
		return;
	}
	
	if(is_page() && $post->ID == $blog_page){
		$sidebar = 'right_sidebar ';

		$sidebar = ($get_template == 'template-full.php') ? 'full_width ' : $sidebar;
		$sidebar = ($get_template == 'template-left-sidebar.php') ? 'left_sidebar ' : $sidebar;
		
		$body_class = 'class="' .$sidebar.$is_homepage_slider.$teaser_style.$overflow. ' ' .$color_scheme. '"';
		echo $body_class;
		
		return;
	}
	
	if(is_single()){
		$sidebar = 'right_sidebar ';
		
		$portfolio_post = get_post_meta($post->ID, 'portfolio_full_img', true);
		$portfolio_layout = get_post_meta($post->ID, 'portfolio_post_layout', true);
		$get_template = get_post_meta($blog_page, '_wp_page_template', true);

		$sidebar = ($get_template == 'template-full.php') ? 'full_width ' : $sidebar;
		$sidebar = ($get_template == 'template-left-sidebar.php') ? 'left_sidebar ' : $sidebar;
		$sidebar = ($portfolio_post && !$portfolio_layout) ? 'full_width ' : $sidebar;
		
		$body_class = 'class="' .$sidebar.$is_homepage_slider.$teaser_style.$overflow. ' ' .$color_scheme. '"';
		echo $body_class;
		
		return;
	}
	
	if(is_archive() || is_search() || is_404()){
		$is_home_slider = ($home_slider_page) ? $is_homepage_slider .' ' : '';
		
		$body_class = 'class="right_sidebar ' .$is_home_slider.$teaser_style.$overflow. ' ' .$color_scheme. '"';
		echo $body_class;
		
		return;
	}
}


function is_webtreats_portfolio_gallery($id){
	$pg = get_page($id);
	
	if ( stristr( $pg->post_content, '[portfolio' )) {
		$search = "@(?:<p>)*\s*\[portfolio\s*\s*cat\s*=\s*(\w+|^\+)\s*\s*column\s*=\s*(\w+|^\+)\s*\s*link\s*=\s*(\w+|^\+)\s*\s*max\s*=\s*(\w+|^\+)\]\s*(?:</p>)*@i";
		if	(preg_match_all($search, $pg->post_content, $matches)) {
			if (is_array($matches)) {
				return true;
			}
		}
	}
	
	return false;
}


function webtreats_google_analytics(){
	global $analytics_code;
	
	if($analytics_code){
		
		echo "<script type=\"text/javascript\">
		/* <![CDATA[ */
		  var _gaq = _gaq || [];
		  _gaq.push(['_setAccount', '" .$analytics_code. "']);
		  _gaq.push(['_trackPageview']);

		  (function() {
		    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
		    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
		    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
		  })();
		/* ]]> */
		</script>";
	}
}


/* 
 * PHP integration for No Spam v1.3
 * by Mike Branski (www.leftrightdesigns.com)
 * mikebranski@gmail.com
 *
 * Copyright (c) 2008 Mike Branski (www.leftrightdesigns.com)
 *
 * NOTE: This script is for integrating your dynamic PHP content with No Script.
 *       Download No Spam at www.leftrightdesigns.com/library/jquery/nospam/
 *
 */
function webtreats_nospam($email, $filterLevel = 'normal')
{
	$email = strrev($email);
	$email = preg_replace('[@]', '//', $email);
	$email = preg_replace('[\.]', '/', $email);

	if($filterLevel == 'low')
	{
		$email = strrev($email);
	}

	return $email;
}

?>